//
//  BKMain_RecommendAndClassifyCell.m
//  bestkeep
//
//  Created by utouu666 on 17/2/23.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BKMain_RecommendAndClassifyCell.h"
#import "BKMainCommon.h"

@interface BKMain_RecommendAndClassifyCell()

@property (nonatomic, strong) UILabel *goodsNameLabel;
@property (nonatomic, strong) UILabel *goodsPriceLabel;
@property (nonatomic, strong) BKMain_StrikeLabel *goodsMarketPriceLabel;
@property (nonatomic, strong) UIImageView *goodsImgeView;
@property (nonatomic, strong) UIImageView *CrossBorderAmoyImgView;//跨境淘
@property (nonatomic, strong) UIImageView *willSaleImgV;//即将上线
@property (nonatomic, strong) UIView *leftView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIView *bgView;
@property(nonatomic,strong)BKCommonTypeGoodsModel *model;
@end

@implementation BKMain_RecommendAndClassifyCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor =[UIColor whiteColor];
        [self createUI];
        [self layout];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
        [self addGestureRecognizer:tap];
    }
    return self;
}
-(void)tapAction{
    
#pragma mark------此处是没有动画的跳转-----------------
//    BKMainViewController *mainPage  = (BKMainViewController *)self.nextResponder.nextResponder.nextResponder;
//    Mediator *mediator = [Mediator sharedInstance];
//    UIViewController *detailVC = [mediator BKGD_goodsDetailViewController:_model.goodsId saleTypes:_model.saleTypes];
//    detailVC.view.backgroundColor = [UIColor whiteColor];
//    [mainPage.navigationController pushViewController:detailVC animated:YES];
    
#pragma mark------此处是有动画的跳转---------------
  

    [[NSNotificationCenter defaultCenter] postNotificationName:@"HideNaviSearchNotion" object:@"BK_main_Hide"];
    
    CGRect goodsFrame = [self convertRect:self.goodsImgeView.frame toView:self.superview.superview];
    BKMainViewController *mainPage  = (BKMainViewController *)self.nextResponder.nextResponder.nextResponder;
    [BKMainCommon goodsDetailAnimationWithFrame:goodsFrame goodsId:_model.goodsId saleTypes:_model.saleTypes goodsCoverImg:_model.goodsCoverImg ViewController:mainPage];
    
}

-(void)createUI{
    
    self.bgView = [UIView new];
    self.bgView.backgroundColor =[UIColor whiteColor];
    [self addSubview:self.bgView];
    //商品图片
    self.goodsImgeView = [[UIImageView alloc]init];
    self.goodsImgeView.image = [UIImage imageNamed:@"BKMain_newGoods"];
    [self.bgView addSubview:self.goodsImgeView];
    
    //即将上架标志
    self.willSaleImgV = [[UIImageView alloc]init];
    self.willSaleImgV.image = [UIImage imageNamed:@"BKMain_goodsWillLine"];
    self.willSaleImgV.hidden = YES;
    [self.goodsImgeView addSubview:self.willSaleImgV];
    //商品描述
    self.goodsNameLabel = [[UILabel alloc]init];
    self.goodsNameLabel.numberOfLines = 1;
    self.goodsNameLabel.textColor = RGB(68, 68, 68);
    self.goodsNameLabel.font = [UIFont systemFontOfSize:14.0];
    self.goodsNameLabel.backgroundColor = [UIColor whiteColor];
    self.goodsNameLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    [self.bgView addSubview:self.goodsNameLabel];
    
    //全球购标志
    self.CrossBorderAmoyImgView = [[UIImageView alloc]init];
    self.CrossBorderAmoyImgView.hidden = YES;
    self.CrossBorderAmoyImgView.image = [UIImage imageNamed:@"BKMain_GlobalPurchase"];
    [self.bgView addSubview:self.CrossBorderAmoyImgView];
    
    //商品价格
    self.goodsPriceLabel = [[UILabel alloc]init];
    self.goodsPriceLabel.textColor =  RGB(242, 18, 39);
    self.goodsPriceLabel.font = [UIFont systemFontOfSize:16.0];
    self.goodsPriceLabel.backgroundColor = [UIColor whiteColor];
    [self.bgView addSubview:self.goodsPriceLabel];
    
    self.goodsMarketPriceLabel = [BKMain_StrikeLabel new];
    self.goodsMarketPriceLabel.font = [UIFont systemFontOfSize:12];
    self.goodsMarketPriceLabel.textColor = RGB(178, 178, 178);
    [self.bgView addSubview:self.goodsMarketPriceLabel];
    
    
    self.leftView = [UIView new];
    self.leftView.backgroundColor = RGB(218, 218, 218);
    [self.bgView addSubview:self.leftView];
    
    self.bottomView = [UIView new];
    self.bottomView.backgroundColor = RGB(218, 218, 218);
    [self.bgView addSubview:self.bottomView];
    
    
}
-(void)layout {
    
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.left.equalTo(self.mas_left);
        make.bottom.equalTo(self.mas_bottom);
        make.right.equalTo(self.mas_right);
    }];
    
    [_goodsImgeView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.mas_top).offset(22.5);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(self.bounds.size.width-47, self.bounds.size.width-47));
    }];
    
    [_willSaleImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left);
        make.top.mas_equalTo(self.mas_top);
        make.height.mas_equalTo(@(38));
        make.width.mas_equalTo(@(38));
    }];
    
    [_CrossBorderAmoyImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.goodsImgeView.mas_bottom).offset(10);
        make.left.mas_equalTo(self.mas_left).offset(14);
        make.size.mas_equalTo(CGSizeMake(42, 15));
    }];
    
    
    [_goodsNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.goodsImgeView.mas_bottom).offset(10);
        make.left.mas_equalTo(self.mas_left).offset(14);
        make.right.mas_equalTo(self.mas_right).offset(-14);
        make.height.equalTo(@20);
    }];
    
    [_goodsPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.mas_left).offset(14);
        make.top.mas_equalTo(self.goodsNameLabel.mas_bottom).offset(5);
        make.height.mas_equalTo(@(15));
    }];
    
    [_goodsMarketPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.goodsPriceLabel.mas_right).offset(5);
        make.right.mas_equalTo(self.mas_right).offset(-10);
        make.bottom.mas_equalTo(self.goodsPriceLabel.mas_bottom).offset(-2);
        make.height.mas_equalTo(@(10));
    }];
    
    [_leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left);
        make.top.equalTo(self.bgView.mas_top);
        make.bottom.equalTo(self.bgView.mas_bottom);
        make.width.equalTo(@0.5);
    }];
    
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left);
        make.right.equalTo(self.bgView.mas_right);
        make.bottom.equalTo(self.bgView.mas_bottom);
        make.height.equalTo(@0.5);
    }];
}

- (void)fillData:(id)data{
    if (!data) {
        return;
    }
    NSDictionary *dic = data;
    _model = [[BKCommonTypeGoodsModel alloc]init];
    [_model setValuesForKeysWithDictionary:dic];
    [_goodsImgeView load:_model.goodsCoverImg  placeholderImage:[UIImage imageNamed:@"BKMain_newGoods"]];
    _goodsNameLabel.text = _model.goodsName;
    //跨境淘
    if ([_model.channelType integerValue] == 2) {
        self.CrossBorderAmoyImgView.hidden = NO;
        [self setFirstLineTextIndentWithContent:self.goodsNameLabel firstLineHeadIndent:45];
    }
    
    else
    {
        self.CrossBorderAmoyImgView.hidden = YES;
        [self setFirstLineTextIndentWithContent:self.goodsNameLabel firstLineHeadIndent:0];
    }
    //即将上线
    if ([_model.status integerValue] == 4) {
        
        self.willSaleImgV.hidden = NO;
    }
    else
    {
        self.willSaleImgV.hidden = YES;
    }
    //价格
    if ([_model.upText isEqualToString:@""] || _model.upText == nil) {
        
        _model.upText = @" ";
    }
    
    _goodsPriceLabel.attributedText = [BKMainCommon specialStringStyle:[NSString stringWithFormat:@"¥%@%@", _model.goodsPrice, _model.upText] fontOfSize:12];
    CGSize size = [self sizeWithContentString:_goodsPriceLabel.text];
    [_goodsPriceLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.mas_left).offset(14);
        make.width.mas_equalTo(@(size.width));
        make.top.mas_equalTo(self.goodsNameLabel.mas_bottom).offset(5);
        make.height.mas_equalTo(@(15));
    }];
    
    _goodsMarketPriceLabel.attributedText = [self setStrikethroughStyle:[NSString stringWithFormat:@"￥%@",_model.goodsMarketPrice]];
}

+(CGSize)calculateSize:(id)data{
    if(!data) {
        return CGSizeMake(0, 0);
    }
    return CGSizeMake(SCREENWIDTH/2.0, 227*SCREENWIDTH/375);
}


-(CGSize)sizeWithContentString:(NSString *)content{
    
    UIFont * font = [UIFont systemFontOfSize:16];
    CGSize  size = CGSizeMake(1000, 15);
    
    return  [self sizeWithContentString:content textFont:font size:size];
}

-(CGSize)sizeWithContentString:(NSString *)content textFont:(UIFont *)font size:(CGSize)contentSize
{
    
    NSDictionary * fontDict = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil];
    CGRect rect = [content boundingRectWithSize:contentSize options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDict context:nil];
    
    return rect.size;
}

// 划线
-(NSAttributedString *)setStrikethroughStyle:(NSString *)text
{
    NSDictionary *dict = @{NSStrikethroughStyleAttributeName:@1};
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:text attributes:dict];
    return string;
}

//价格的中“起”的颜色的设置
-(NSAttributedString *)setTextColor:(NSString *)text rang:(NSRange)range {
    
    
    NSMutableAttributedString *ColorStr = [[NSMutableAttributedString alloc]initWithString:text];
    NSDictionary *dict = @{NSForegroundColorAttributeName:[UIColor redColor]};
    [ColorStr setAttributes:dict range:range];
    return ColorStr;
    
}

//设置首行缩进
-(void)setFirstLineTextIndentWithContent:(UILabel*)contentLabel firstLineHeadIndent:(CGFloat)value
{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contentLabel.text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:3];
    paragraphStyle.firstLineHeadIndent= value;
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [contentLabel.text length])];
    contentLabel.attributedText = attributedString;
    contentLabel.lineBreakMode = NSLineBreakByTruncatingTail;
}

@end
